<?php

namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\User;
use think\Db;
use think\Validate;
use think\Loader;
use \app\admin\model\ManystoreApply as ManystoreApplys;
use \app\admin\model\GoodsCategory;

/**
 * 商家入駐
 */
class Manystoreaapply extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    protected $amodel = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->amodel = new ManystoreApplys;
    }

    /**
     * @MethodName 獲取經營類目列表
     * @Author     Allen
     * @DateTime   2021-10-04T14:59:51+0800
     * @param      string
     * @return     [type]
     */
    public function getGoodsCategoryList($value='')
    {
        $model = new GoodsCategory;
        $list = $model
                ->field('id,name,createtime')
                ->where(['type'=>1])
                ->order('weigh desc,id desc')
                ->select();

        $this->success('請求成功',$list);
    }

    /**
     * @MethodName 商家入駐
     * @Author     Allen
     * @DateTime   2021-10-05T17:12:04+0800
     * @param      string
     * @return     [type]
     */
    public function apply($value='')
    {
        $manystore_shop = db('manystore_shop')
                            ->where(['user_id'=>$this->auth->id])
                            ->find();

        if ( $manystore_shop ) {
            $this->error('您已經申請開通一家店鋪,請勿重複申請',[],205);
        }
        $manystore_apply = $this->amodel
                        ->where(['user_id'=>$this->auth->id])
                        ->order('id desc')
                        ->find();

        if ( $manystore_apply ) {
            switch ($manystore_apply['status']) {
                case '1':
                    $this->error('請耐心等待審核',[],204);
                    break;
                case '2':
                    $this->error('申請已通過,請勿重複提交',[],204);
                    break;
            }
        }
        
        $rq_data = $this->request->param();
        $validate = Loader::validate('Manystoreaapply');
        if (!$validate->scene('apply')->check($rq_data)) {
            $this->error($validate->getError());
        }

        $insert_arr = [];
        $insert_arr['user_id'] = $this->auth->id;
        $insert_arr['name'] = $rq_data['name'];
        $insert_arr['mobile'] = $rq_data['mobile'];
        $insert_arr['cat_id'] = $rq_data['cat_id'];
        $insert_arr['id_card'] = $rq_data['id_card'];
        $insert_arr['id_card_positive_images'] = $rq_data['id_card_positive_images'];
        $insert_arr['id_card_back_images'] = $rq_data['id_card_back_images'];
        $insert_arr['return_permit'] = $rq_data['return_permit'];
        $insert_arr['return_permit_positive_images'] = $rq_data['return_permit_positive_images'];
        $insert_arr['return_permit_back_images'] = $rq_data['return_permit_back_images'];
        $insert_arr['business_license_images'] = $rq_data['business_license_images'];
        $insert_arr['address'] = $rq_data['address'];
        $insert_arr['shop_name'] = $rq_data['shop_name'];
        $insert_arr['avatar'] = $rq_data['avatar'];
        if ( $manystore_apply ) {

            $insert_arr['audittime'] = 0;
            $insert_arr['audreason'] = '';
            $insert_arr['updatetime'] = time();
            $insert_arr['status'] = '1';
            $status = $this->amodel
                    ->where(['id'=>$manystore_apply['id']])
                    ->update($insert_arr);
        }else{
            $insert_arr['createtime'] = time();
            $status = $this->amodel->insert($insert_arr);
        }


        if ( !$status ) {
            $this->error('操作失敗');
        }

        $this->success('操作成功');
    }

    /**
     * @MethodName 商家申請入駐詳情
     * @Author     Allen
     * @DateTime   2021-10-05T17:20:44+0800
     * @param      string
     */
    public function applyInfo($value='')
    {

        $info = [];
        $info_buf = [];
        // 審核狀態0未申請1待審核2通過3拒絕
        $info_buf['status'] = 0;
        $info_buf['createtime'] = 0;
        $info_buf['audreason'] = '';
        $info_buf['audittime'] = 0;
        $info_buf['name'] = '';
        $info_buf['mobile'] = '';
        $info_buf['cat_id'] = 0;
        $info_buf['cat_name'] = '';
        $info_buf['id_card'] = '';
        $info_buf['id_card_positive_images'] = '';
        $info_buf['id_card_back_images'] = '';
        $info_buf['return_permit'] = '';
        $info_buf['return_permit_positive_images'] = '';
        $info_buf['return_permit_back_images'] = '';
        $info_buf['business_license_images'] = '';
        $info_buf['address'] = '';
        $info_buf['shop_name'] = '';
        $info_buf['avatar'] = '';
        $info_buf['username'] = '';
        $info_buf['password'] = '';

        $info = $info_buf;

        $apply = $this->amodel
                        ->field('status,createtime,audreason,audittime,name,mobile,cat_id,name,id_card,id_card_positive_images,id_card_back_images,return_permit,return_permit_positive_images,return_permit_back_images,business_license_images,address,shop_name,avatar,username,password')
                        ->where(['user_id'=>$this->auth->id])
                        ->order('id desc')
                        ->find();
                        
        if ( $apply ) {
            $apply['cat_name'] = db('goods_category')
                                ->where(['id'=>$apply['cat_id']])
                                ->value('name');

            $apply['audreason'] = $apply['audreason']?$apply['audreason']:'';
            $info = $apply;
        }

        $manystore_shop = db('manystore_shop')
                        ->where(['user_id'=>$this->auth->id])
                        ->find();
        
        if ( $manystore_shop ) {
            $info['status'] = 2;
        }

        if ( !$manystore_shop && isset($apply) && $apply['status'] == '2' ) {
            $info = $info_buf;
        }

        $this->success('請求成功',$info);
    }

}
